package com.sentiment.database.bean;

import java.util.ArrayList;
import java.util.List;

import org.bson.Document;import org.bson.types.ObjectId;
import org.bson.types.ObjectId;

import com.sentiment.database.dbUtil.DbUtil;

public class User_Permissions {
	
	public static final String dbName = "UserDB";
	public static final String collection = "User_Permissions";
	
	private ObjectId _id;
	private String user_id;
	private List<String> permissions_id;

	public User_Permissions(String user_id,
			List<String> permissions_id) {
		super();
		this.user_id = user_id;
		this.permissions_id = permissions_id;
	}
	
	/**
	 * 转换为无 id 的 Document 类型
	 * @return
	 */
	public Document toDocument(){
		return new Document("user_id",user_id).append("permissions_id", permissions_id);
	}
	
	public Document toDocument_id(){
		return new Document("_id",_id).append("user_id",user_id).append("permissions_id", permissions_id);
	}
	
	public User_Permissions(Document doc){
		super();
		_id = doc.getObjectId("_id");
		user_id = doc.getString("user_id");
		permissions_id = (List<String>)doc.get("permissions_id");
	}
	
	public User_Permissions() {
		// TODO Auto-generated constructor stub
	}

	public String get_id() {
		return _id.toString();
	}
	public void set_id(String _id) {
		this._id = new ObjectId(_id);
	}
	public String getUser_id() {
		return user_id;
	}
	public void setUser_id(String user_id) {
		this.user_id = user_id;
	}
	public List<String> getpermissions_id() {
		return permissions_id;
	}
	public void setpermissions_id(List<String> permissions_id) {
		this.permissions_id = permissions_id;
	}
	@Override
	public String toString() {
		return "User_Permissions [_id=" + _id + ", user_id=" + user_id
				+ ", permissions_id=" + permissions_id + "]";
	}
	
	/**
	 * 根据用户 id 查找并返回 用户和用户权限的对应关系 User_Permissions
	 * @param user_id
	 * @return
	 */
	public User_Permissions getUser_PermissionsByUser_id(String user_id) {
		DbUtil db = new DbUtil();
		db.openConnection(dbName, collection);
		
		User_Permissions user_pers = new User_Permissions(user_id,null);
		String[] keys = {"user_id"};
		Document t = user_pers.toDocument();
		t = db.query(t, keys);
		
		db.closeConnection();
		
		if(t == null)
			return null;
		
		return new User_Permissions(t);
	}
	
	/**
	 * 用于测试时向数据库中写入数据
	 */
	public void test() {
		
		DbUtil db = new DbUtil();
		db.openConnection(dbName, collection);
		
		List<String> users = new ArrayList<>();
		users.add("user");
		users.add("user_pay");
		users.add("admin");
		for(int j = 0; j < users.size(); j++){
		
			User user = new User();
			user = user.getUserByUserCode(users.get(j));
			User_Roles user_roles = new User_Roles();
			user_roles = user_roles.getUser_RolesByUser_id(user.get_id());
			List<String> roles_id = new ArrayList<>();
			roles_id = user_roles.getRoles_id();
			List<String> pers = new ArrayList<>();
			List<String> per = new ArrayList<>();
			Role_Permissions role_pers = new Role_Permissions();
			for(int i = 0; i < roles_id.size(); i++){
				role_pers = role_pers.getRole_PermissionsByRole_id(roles_id.get(i));
				per = role_pers.getPermission_id();
				if(pers.size() != 0)
					pers.removeAll(per);
				pers.addAll(per);
				per.clear();
			}
			User_Permissions user_per = new User_Permissions(user.get_id(),pers);
			Document t = user_per.toDocument();
			db.insert(t);
		
		}
		
		db.closeConnection();
		
	}
	
	/**
	 * 该方法通过角色重新设置用户的权限 id
	 * 使用前需判断 roles 存在数据库中
	 * @param user_id
	 */
	public boolean updatePermissionByRoles(List<Role> roles){
		
		User_Permissions newuser_pers = this;
		List<String> pers_id = new ArrayList<>();
		DbUtil db = new DbUtil();
		
		for(int i = 0; i < roles.size(); i++){
			Role_Permissions role_pers = new Role_Permissions();
			List<Permission> perlist = role_pers.getPermissionListByRole_id(roles.get(i).get_id());
			
			for(int j = 0; j < perlist.size(); j++){
				String per_id = perlist.get(j).get_id();
				if(!pers_id.contains(per_id))
					pers_id.add(per_id);
			}
		}
		newuser_pers.setpermissions_id(pers_id);
		
		String []nkey = {"permissions_id"};
		String []okey = {"_id"};
		db.openConnection(User_Permissions.dbName, User_Permissions.collection);
		Boolean updated = db.update(this.toDocument_id(), okey, newuser_pers.toDocument_id(), nkey);
		db.closeConnection();
		
		return updated;
	}
	
	
	/**
	 * 删除 User_Permissions 表中所有 Permission
	 * @param per
	 * @return
	 */
	public boolean deletePermission(Permission per){
		
		DbUtil db = new DbUtil();
		db.openConnection(dbName, collection);
		
		boolean remove = db.remove("permissions_id", per.get_id());
		
		db.closeConnection();
		
		return remove;
	}
	
	public static void main(String[] args) {
		User_Permissions user_pers = new User_Permissions();
		user_pers.test();
	}
	
}
